#include <cppunit/config/SourcePrefix.h>

#include "PKBStubForParentTest.h"
#include "PatternTable.h"
#include "TestPatternTable.h"

void PatternTableTest::setUp() {
	cout << "Test PatternTable setUp()\n";
}

void PatternTableTest::tearDown() {
}

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( PatternTableTest );

// method to test the constructor
void PatternTableTest::testConstructor() {
	// Just to ensure it does not produce errors
	PatternTable pTable;
}

void PatternTableTest::testAddPattern() {
	PatternTable pTable;
	
	PKBStubForParentTest pkbStub;
	mQueryExpParser = new QueryExpressionParser(&pkbStub);
	TreeNode* expr1 = NULL;
	TreeNode* expr2 = NULL;

	expr1 = mQueryExpParser->convertExpressionToTree("y+z");
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_ASSIGN, "a", "x", true, expr1, false, NULL, false));
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_IF, "i", "y", true, NULL, false, NULL, false));
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_ASSIGN, "a", "", false, expr1, false, NULL, false));
}

void PatternTableTest::testAddPatternWithSynArgument() {
	PatternTable pTable;
	std::vector<RelationshipArgumentType> arg1SynTypes;
	
	PKBStubForParentTest pkbStub;
	mQueryExpParser = new QueryExpressionParser(&pkbStub);
	TreeNode* expr1 = NULL;
	TreeNode* expr2 = NULL;

	expr1 = mQueryExpParser->convertExpressionToTree("y+z");
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_ASSIGN, "a", "x", true, expr1, false, NULL, false));
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_IF, "i", "y", true, NULL, false, NULL, false));
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_ASSIGN, "a", "v", true, SYNONYM_VARIABLE, expr1, false, NULL, false));
}

void PatternTableTest::testGetNumberOfEntries() {
	PatternTable pTable;
	
	PKBStubForParentTest pkbStub;
	mQueryExpParser = new QueryExpressionParser(&pkbStub);
	TreeNode* expr1 = NULL;
	TreeNode* expr2 = NULL;

	expr1 = mQueryExpParser->convertExpressionToTree("y+z");
	// Insert some entries
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_ASSIGN, "a", "x", true, expr1, false, NULL, false));
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_IF, "i", "y", true, NULL, false, NULL, false));
	CPPUNIT_ASSERT(true == pTable.addPattern(PATTERN_ASSIGN, "a", "", false, expr1, false, NULL, false));

	CPPUNIT_ASSERT_EQUAL(3, pTable.getNumberOfEntries());
}